﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EMR_System.Forms
{
    public partial class EMRForm : Form
    {
        public EMRForm()
        {
            InitializeComponent();
        }

        private void EMRForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'eMR_SystemDataSet1.Patient' table. You can move, or remove it, as needed.
            this.patientTableAdapter.Fill(this.eMR_SystemDataSet1.Patient);
            getAppointments();

            if (Session.Instance.User.Type == UserType.Nurse)
            {
                addPatientMenuItem.Enabled = false;
                addAppointmentBtn.Enabled = false;
                addAppointmentMenuItem.Enabled = false;
                tabControl1.TabPages.RemoveAt(0);
            }

            string userType;

            if (Session.Instance.User.Type == UserType.Receptionist)
                userType = "Receptionist";
            else if (Session.Instance.User.Type == UserType.Nurse)
                userType = "Nurse";
            else
                userType = "Doctor";

            usernameBtn.Text = Session.Instance.User.Username + " (" + userType + ")";
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EMR_System.Controllers.UserController.Logoff();
            this.Close();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Session.Instance.User.Username = null;
            Session.Instance.User.Password = null;
            this.Close();
        }

        private void addPatientBtn_Click(object sender, EventArgs e)
        {
            new PatientForm().ShowDialog();
        }

        private void addPatientMenuItem_Click(object sender, EventArgs e)
        {
            new PatientForm().ShowDialog();
        }

        private void addAppointmentBtn_Click(object sender, EventArgs e)
        {
            new AppointmentForm().ShowDialog();
        }

        private void addAppointmentMenuItem_Click(object sender, EventArgs e)
        {
            new AppointmentForm().ShowDialog();
        }

        private void changePasswordMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void getAppointmentsBtn_Click(object sender, EventArgs e)
        {
            getAppointments();            
        }

        private void getAppointments()
        {
            if (Session.Instance.User.Type == UserType.Receptionist)
            {
                this.appointmentTableAdapter.FillFutureAppointments(this.eMR_SystemDataSet.Appointment, DateTime.Now.ToString("MM/dd/yyyy"));
            }
            else if (Session.Instance.User.Type == UserType.Nurse)
            {
                this.appointmentTableAdapter.FillCurrentAppointments(this.eMR_SystemDataSet.Appointment, DateTime.Now.ToString("MM/dd/yyyy"));
            }
        }

        // Appointments
        private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (Session.Instance.User.Type == UserType.Nurse)
            {
                int row = dataGridView1.SelectedCells[0].RowIndex;
                int apptID = Convert.ToInt32(dataGridView1.Rows[row].Cells[0].Value);

                new Forms.AppointmentForm(Controllers.AppointmentController.SelectAppointment(apptID)).Show(this);
            }
        }

        // Patients
        private void dataGridView2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int row = dataGridView2.SelectedCells[0].RowIndex;
            int patientID = Convert.ToInt32(dataGridView2.Rows[row].Cells[0].Value);

            new Forms.PatientForm(Controllers.PatientController.GetPatient(patientID)).ShowDialog();
        }
    }
}
